//
//  LPPGuideController.m
//  Lottery
//
//  Created by 刘盼 on 16/1/24.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPGuideController.h"
#import "LPPGuideCell.h"
#import "UIView+LPPFrame.h"
#import "LPPTabBarController.h"

#define KScreenSize [UIScreen mainScreen].bounds.size
@interface LPPGuideController ()

@property(nonatomic,strong)UIImageView *guideImageView;

@property(nonatomic,strong)UIImageView *largeTextImageView;

@property(nonatomic,strong)UIImageView *smallTextImageView;
//记录当前滑动到哪页
@property(nonatomic,assign) int page;
@end

@implementation LPPGuideController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    //滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //分页
    self.collectionView.pagingEnabled = YES;
    //弹簧效果
    self.collectionView.bounces = NO;
    
    [self.collectionView registerClass:[LPPGuideCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    UIImageView *lineImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLine"]];
    
    lineImageView.x = -202;
    
    [self.collectionView addSubview:lineImageView];
    
    self.largeTextImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    
    self.largeTextImageView.y = KScreenSize.height *0.75;
    
    self.smallTextImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    
    self.smallTextImageView.y = KScreenSize.height *0.85;
    
    self.guideImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide1"]];
    
    [self.collectionView addSubview:self.guideImageView];
    [self.collectionView addSubview:self.largeTextImageView];
    [self.collectionView addSubview:self.smallTextImageView];
    
    UIButton *entryBtn = [[UIButton alloc]init];
    
    [entryBtn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
    
    [entryBtn sizeToFit];
    
    entryBtn.x = (KScreenSize.width - entryBtn.width) * 0.5 + KScreenSize.width * 3;
    entryBtn.y = CGRectGetMaxY(self.smallTextImageView.frame) + 10;
    
    [entryBtn addTarget:self action:@selector(entryMainVc) forControlEvents:UIControlEventTouchUpInside];
    
    [self.collectionView addSubview:entryBtn];
}

#pragma mark----------进入主控制器-----------

-(void)entryMainVc {
    
    UIApplication *app = [UIApplication sharedApplication];
    
    UIWindow * window = app.keyWindow;
    
    LPPTabBarController *tabbar = [[LPPTabBarController alloc]init];
    
    window.rootViewController = tabbar;
    
}
#pragma mark----------重写初始化函数-----------
//完成流水布局
-(instancetype)init {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = KScreenSize;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}

#pragma mark----------<UICollectionViewDelegate>---------
//减速完成之后调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
   
    CGFloat offsetX = scrollView.contentOffset.x;
    
    int page = offsetX / KScreenSize.width;
    //左滑
    if (page > self.page) {
      
        self.guideImageView.x = offsetX + KScreenSize.width;
        
        self.largeTextImageView.x = offsetX + KScreenSize.width;
        
        self.smallTextImageView.x = offsetX + KScreenSize.width;
    }else{
        //右滑
        self.guideImageView.x = offsetX - KScreenSize.width;
        
        self.largeTextImageView.x = offsetX - KScreenSize.width;
        
        self.smallTextImageView.x = offsetX - KScreenSize.width;
    }
    self.page = page;
    
    NSString *guideImageName = [NSString stringWithFormat:@"guide%d",page + 1];
    
    self.guideImageView.image = [UIImage imageNamed:guideImageName];
    
    NSString *largeTextImageName = [NSString stringWithFormat:@"guideLargeText%d",page + 1];
    
    self.largeTextImageView.image = [UIImage imageNamed:largeTextImageName];
    
    NSString *smallTextImageName = [NSString stringWithFormat:@"guideSmallText%d",page + 1];
    
    self.smallTextImageView.image = [UIImage imageNamed:smallTextImageName];
    
    [UIView animateWithDuration:0.3 animations:^{
       
        self.guideImageView.x = offsetX;
        
        self.largeTextImageView.x = offsetX;
        
        self.smallTextImageView.x = offsetX;
    }];
}
#pragma mark----------<UICollectionViewDataSource>---------

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LPPGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.row + 1];
    
    cell.imageName = imageName;
    
    return cell;
}
@end
